package mycoffee.order.adapter;

import java.util.ArrayList;

import mycoffee.order.R;
import mycoffee.order.model.SubCategory;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SubCategoryAdapter extends BaseAdapter {
	/** list of categories. */
	private ArrayList<SubCategory> mItems = new ArrayList<SubCategory>();
	/** Interface to global information about an application environment. */
	protected Context mContext;
	/** used to instantiate layout XML file into its corresponding View objects. */
	private LayoutInflater mInfalInflater;

	/** position of subcategory that is selected. */
	private int mCurrentSubCategoryIndex = 0;

	public SubCategoryAdapter(Context context,
			ArrayList<SubCategory> subCategories) {
		mContext = context;
		mInfalInflater = LayoutInflater.from(context);
		mItems = subCategories;
	}

	@Override
	public int getCount() {
		if (mItems != null) {
			return mItems.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mItems != null && mItems.size() > position && position >= 0) {
			return mItems.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInfalInflater.inflate(R.layout.subcategory_item,
					null);
		}

		// update data.
		if (mItems == null || position < 0 || mItems.size() <= position) {
			return null;
		}
		((TextView) convertView).setText(mItems.get(position)
				.getNameFollowCurrentLanguage());
		if (mCurrentSubCategoryIndex == position) {
			convertView.setBackgroundColor(mContext.getResources().getColor(
					R.color.subcategory_background_over_color));
		} else {
			convertView.setBackgroundColor(mContext.getResources().getColor(
					R.color.trasparent_color));
		}
		return convertView;
	}

	public void setItems(ArrayList<SubCategory> items) {
		mItems = items;
		mCurrentSubCategoryIndex = 0;
		notifyDataSetChanged();
	}

	/**
	 * @param currentSubCategoryIndex
	 *            the currentSubCategoryIndex to set
	 */
	public void setCurrentSubCategoryIndex(int currentSubCategoryIndex) {
		mCurrentSubCategoryIndex = currentSubCategoryIndex;
		notifyDataSetChanged();
	}

}
